package labox.innovation.gameserver;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 此类处理游戏的唯一时间计数器
 * 
 * @version $Revision: 1.1.4.8 $ $Date: 2005/04/06 16:13:24 $
 */
public class GameTimeController extends Thread {

	public static final int TICKS_PER_SECOND = 10; // not able to change this without checking through code

	public static final int MILLIS_IN_TICK = 1000 / TICKS_PER_SECOND;

	protected static volatile int _gameTicks;

	public static final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间 

	protected static long _gameStartTime;

	/**
	 * one ingame day is 240 real minutes
	 */
	public static GameTimeController getInstance() {
		return SingletonHolder._instance;
	}

	private GameTimeController() {

		super("GameTimeController");
		_gameStartTime = System.currentTimeMillis();
		setDaemon(true);
		setPriority(NORM_PRIORITY);
		start();

	}

	public int getGameTime() { //得到游戏世界当前秒
		return (_gameTicks / TICKS_PER_SECOND );
	}

	public static int getGameTicks() {
		return _gameTicks;
	}

	public int getGameHours() //得到游戏世界当前运行多少小时
	{
		return (getGameTime() / 60) % 24;
	}

	public int getGameMinute() //得到游戏世界当前运行多少分钟
	{
		return getGameTime() % 60;
	}

	/**
	 * 得到游戏世界时间和指定时间的差
	 * 
	 * @since 1.0
	 * @return 毫秒
	 */
	public int getDiffToTime(String date) {
		int nowTime = getGameTime() * 1000;
		long result = 0;
		try {
			result = nowTime - d.parse(date).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (int) result;
	}

	/**
	 * 得到指定两时间的差
	 * 
	 * @since 1.0
	 * @return 毫秒
	 */
	public int getDiffToTime(String date1, String date2) {
		long result = 0;
		try {
			long nowTime = d.parse(date1).getTime();
			result = nowTime - d.parse(date2).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (int) result;
	}

	/**
	 * 得到游戏世界时间 HH:mm
	 * 
	 * @since 1.0
	 * @return
	 */
	public String getGameTimeHHMM() {
		Calendar c = Calendar.getInstance();
		int t = getGameTime();
		int h = t / 60;
		int m = t % 60;
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		Date da = c.getTime();

		return format.format(Calendar.getInstance().getTime());
	}

	/**
	 * 得到现在时间
	 * 
	 * @since 1.0
	 * @return Date
	 */
	public Date getNowDate() {
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		return date;
	}

	@Override
	public void run() {
		long runtime;
		int sleepTime;

		for (;;) {

			try {
				runtime = System.currentTimeMillis() - _gameStartTime; // from server boot to now

				_gameTicks = (int) (runtime / MILLIS_IN_TICK); // new ticks value (ticks now)

				runtime = (System.currentTimeMillis() - _gameStartTime) - runtime;

				sleepTime = 1 + MILLIS_IN_TICK - ((int) runtime) % MILLIS_IN_TICK;
				//					
				//					System.out.println("----------_gameTicks----------:"+_gameTicks);
				//					System.out.println("------------now---------------:"+System.currentTimeMillis());
				if (sleepTime > 0)
					Thread.sleep(sleepTime);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder {
		protected static final GameTimeController _instance = new GameTimeController();
	}
}
